package xrz.cuanbao.adapters;

import java.util.ArrayList;
import java.util.List;

import org.xutils.x;
import org.xutils.image.ImageOptions;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import xrz.cuanbao.R;
import xrz.cuanbao.entitys.ImageBanner;

public class ImageAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private List<ImageBanner> images = new ArrayList<ImageBanner>();
	private int Type;

	ImageOptions imageOptions = new ImageOptions.Builder().setIgnoreGif(false)
			.setImageScaleType(ImageView.ScaleType.CENTER).build();

	public ImageAdapter(Context context, int Type) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.Type = Type;
	}
	public ImageAdapter(Context context, int Type,List<ImageBanner> list) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.Type = Type;
		images.addAll(list);
	}

	public void addImages(List<ImageBanner> list) {
		if (list == null || list.size() == 0)
			return;
		images.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return images.size() == 0 ? 0 :Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.image_item, null);
		}
		if(images.size() == 0)
			return convertView;
		int num = 0;
		if (position <= images.size() - 1) {
			num = position;
		} else {
			num = position % images.size();
		}
		x.image().bind(((ImageView) convertView.findViewById(R.id.imgView)), images.get(num).image_src);
		if (Type == 0) {
			((ImageView) convertView.findViewById(R.id.img_voide)).setVisibility(View.GONE);
		} else if (Type == 1) {
			((ImageView) convertView.findViewById(R.id.img_voide)).setVisibility(View.VISIBLE);
		}
		return convertView;
	}
}
